package com.czy.owner.ui.hotshop;

import android.graphics.Bitmap;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.ui.BaseActivity;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

import static com.czy.owner.R.id.webview;
/**
 * Created by asus
 * 加载单纯url路径
 */
public class HotShopWebActivity extends BaseActivity {
    @BindView(webview)
    WebView webView;
    @BindView(R.id.myProgressBar)
    ProgressBar myProgressBar;
    private String title;
    @BindView(R.id.tv_page_title_center)
    TextView tvPageTitleCenter;
    private String url;
    private ArrayList<String> loadHistoryUrls = new ArrayList<String>();
    private String mLastUrl;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_hot_shop_web;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        title = getIntent().getStringExtra("title");
        url = getIntent().getStringExtra("url");
        tvPageTitleCenter.setText(title);
        webView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                WebView.HitTestResult hr = ((WebView) view).getHitTestResult();
                if (hr != null && loadHistoryUrls != null) {
                    if (loadHistoryUrls.size() < 1) {
                        if (loadHistoryUrls.isEmpty() || !loadHistoryUrls.get(loadHistoryUrls.size() - 1).equals(mLastUrl)) {
                            loadHistoryUrls.add(mLastUrl);
                        }
                    }


                }
                return false;
            }
        });
        WebSettings settings = webView.getSettings();
        settings.setAppCacheEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setDatabaseEnabled(true);
        settings.setCacheMode(WebSettings.LOAD_DEFAULT);
        settings.setJavaScriptEnabled(true);
        settings.setLoadWithOverviewMode(true);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setSupportZoom(true);
        webView.loadUrl(url);
        webView.setWebViewClient(new MyWebViewClient());
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    myProgressBar.setVisibility(View.GONE);
                } else {
                    if (View.GONE == myProgressBar.getVisibility()) {
                        myProgressBar.setVisibility(View.VISIBLE);
                    }
                    myProgressBar.setProgress(newProgress);
                }
                super.onProgressChanged(view, newProgress);
            }


        });

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        //判断是否可以返回操作
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
            //过滤是否为重定向后的链接

            if (loadHistoryUrls.size() > 0) {
                loadHistoryUrls.remove(loadHistoryUrls.get(loadHistoryUrls.size() - 1));
                //加载重定向之前的页
                webView.loadUrl(url);
             //   return false;

            } else {
                finish();
            }
        }
        return super.onKeyDown(keyCode, event);

    }

    @Override
    protected void updateViews() {

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        webView.destroy();
    }

    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;


        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);

        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            mLastUrl = url;

        }
    }

    @OnClick(R.id.btn_page_title_left)
    public void onViewClicked() {
        if (webView.canGoBack()) {
            //过滤是否为重定向后的链接
            if (loadHistoryUrls.size() > 0) {
                loadHistoryUrls.remove(loadHistoryUrls.get(loadHistoryUrls.size() - 1));
                //加载重定向之前的页
                webView.loadUrl(url);

            } else {
                finish();
            }
        } else {
            finish();
        }

    }

}